package com.sdry.web.controller.cb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.cb.DepreciationEntity;
import com.sdry.model.cb.ForkliEntity;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.lz.Warehouse;
import com.sdry.service.cb.DepreciationService;
import com.sdry.service.cb.ForkliService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RequestMapping("forkli")
@Controller
public class ForkliController extends BaseController{

    @Autowired
    private ForkliService forkliService;

    @Autowired
    private DepreciationService depreciationService;

    @RequestMapping
    public String index(Model model) throws ParseException {

        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        List<DepreciationEntity> list = depreciationService.getList(format1.format(new Date()), null);
        model.addAttribute("depreciationEntity",list);

        //仓库
        List<Warehouse> warehouseList = iDicMapper.getWarehouseList();
        model.addAttribute("warehouseList",warehouseList);

        return "/cb/forkli";
    }


    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo getList(
            @RequestParam("page") Integer page ,
            @RequestParam("pageSize") Integer pageSize,
            String date,
            Long stockId
    ) {

        PageHelper.startPage(page,pageSize);
        List<ForkliEntity> list = forkliService.getList(stockId,date);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(),Math.toIntExact(pageInfo.getTotal()));
    }

    @RequestMapping("add")
    @ResponseBody
    public Integer add(ForkliEntity fe) throws ParseException {

        return forkliService.add(fe);
    }

    @RequestMapping("del")
    @ResponseBody
    public Integer del (@RequestParam("idArr[]") String[] idArr) {
        return forkliService.delete(idArr);
    }


}
